import 'dart:async';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primaryColor: Colors.green),
      home: ListView2(),
    );
  }
}

class _Home extends StatefulWidget {
  const _Home({Key? key}) : super(key: key);

  @override
  __HomeState createState() => __HomeState();
}

class __HomeState extends State<_Home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text("这个模块时listview"),
      ),
      body: ListView(
        children: [
          ListTile(
            leading:
                Image.asset("res/drawable/drawable-xxhdpi/ic_app_logo.png"),
            title: Text("我是标题1"),
            subtitle: Text("我是标题2"),
            trailing: Image.asset(
                "res/drawable/drawable-xxhdpi/ic_arrow_right_gray_medium.png"),
          ),
        ],
      ),
    );
  }
}

//f分割线
class _Home3 extends StatefulWidget {
  const _Home3({Key? key}) : super(key: key);

  @override
  __Home3State createState() => __Home3State();
}

class __Home3State extends State<_Home3> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text("这个模块时listview"),
      ),
      body: ListView.separated(
        itemCount: 100,
        separatorBuilder: (BuildContext context, int index) {
          return Divider(
            height: 3,
            color: Colors.deepOrange,
          );
        },
        itemBuilder: (BuildContext context, int index) {
          return ListTile(
            leading:
                Image.asset("res/drawable/drawable-xxhdpi/ic_app_logo.png"),
            title: Text("我是标题${index}"),
            subtitle: Text("我是标题2"),
            trailing: Image.asset(
                "res/drawable/drawable-xxhdpi/ic_arrow_right_gray_medium.png"),
          );
        },
      ),
    );
  }
}

//设置方向
class _ListView extends StatefulWidget {
  const _ListView({Key? key}) : super(key: key);

  @override
  _ListViewState createState() => _ListViewState();
}

class _ListViewState extends State<_ListView> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text("这个模块时listview"),
      ),
      body: ListView.separated(
        scrollDirection: Axis.horizontal,
        itemCount: 100,
        separatorBuilder: (BuildContext context, int index) {
          return Divider(
            height: 3,
            color: Colors.deepOrange,
          );
        },
        itemBuilder: (BuildContext context, int index) {
          return Text("我是标题$index");
        },
      ),
    );
    ;
  }
}

//上拉刷新 下来加载更多
class ListView2 extends StatefulWidget {
  const ListView2({Key? key}) : super(key: key);

  @override
  _ListView2State createState() => _ListView2State();
}

class _ListView2State extends State<ListView2> {
  late ScrollController _scrollController;

  List<String> _list = [];

  Future _initData() async {
    Future.delayed(Duration(seconds: 2)).then((value) {
      setState(() {
        _list.clear();
        for (int i = 0; i < 20; i++) {
          _list.add("value$i");
        }
      });
    });
  }

  _addMore() {
    Future.delayed(Duration(seconds: 2)).then((value) {
      setState(() {
        var length = _list.length;
        for (int i = 0; i < 10; i++) {
          _list.add("value${i + length}");
        }
      });
    });
  }

  @override
  void initState() {
    super.initState();
    _initData();
    _scrollController = ScrollController()
      ..addListener(() {
        if (_scrollController.position.pixels ==
            _scrollController.position.maxScrollExtent) {
          if (_list.length > 0 &&  _list.length<=100) {
            setState(() {
              _addMore();
            });
          }
        }
      });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text("上下拉刷新"),
      ),
      body: RefreshIndicator(
        child: ListView.separated(
            controller: _scrollController, //滑动监听
            itemCount: _list.length + 1,
            separatorBuilder: (BuildContext context, int index) {
              return Divider(
                height: 1,
                color: Colors.green,
              );
            },
            itemBuilder: (BuildContext context, int index) {
              if (index == _list.length && _list.length<=100) {
                return CupertinoActivityIndicator(
                  radius: 20,
                );
              }
              if(index == _list.length && _list.length>=100){
                return Text("已经没有更多了");
              }
              return ListTile(
                title: Text("我是标题${_list[index]}"),
              );
            }),
        onRefresh: _initData,
      ),
    );
  }
}
